//
// Created by LiuYou on 2021/10/26.
//

#include <iostream>


static const int BUF_SIZE = 1024;
static FILE* srcFp;
static FILE* destFp;


/*
 * ./MyCopy srcfile destfile
 */
int main(int argc, char** argv) {
    if (argc < 3) {
        fprintf( stderr, "用法: %s /srcfile /destfile\n", argv[0] );
        return 0;
    }
    srcFp = fopen( argv[1], "r" );
    if (srcFp == nullptr) {
        perror( "fopen()" );
        return 1;
    }
    atexit( [] {
        fclose( srcFp );
    } );
    destFp = fopen( argv[2], "w" );
    if (destFp == nullptr) {
        perror( "fopen()" );
        return 1;
    }
    atexit( []() -> void {
        fclose( destFp );
    } );


//    while (true) {
//        int readChar = fgetc( srcFp );
//        if (readChar == EOF) {
//            break;
//        }
//        fputc( readChar, destFp );
//    }

//    char buf[BUF_SIZE]{};
//    while (true) {
//        char* str = fgets( buf, BUF_SIZE, srcFp );
//        if (str == nullptr) {
//            break;
//        }
//        fputs( buf, destFp );
//    }


    char buf[BUF_SIZE]{};
    while (true) {
        size_t readBytes = fread( buf, 1, BUF_SIZE, srcFp );
        if (readBytes == 0) {
            break;
        }
        fwrite( buf, 1, readBytes, destFp );
    }


    return 0;
}
